%--------------------------------------------------------------------------
%   多用户CDMA
%--------------------------------------------------------------------------
clear;clc;
%--------------------------------------------------------------------------
%   产生发射的内容数据
%--------------------------------------------------------------------------
data1 = randi([0 1],1,15);                                                   %发送数据
data2 = randi([0 1],1,15);                                                   %发送数据

%--------------------------------------------------------------------------
%   扩展发射数据
%--------------------------------------------------------------------------
data_exp1 = ones(1,64)'*data1;
data_exp2 = ones(1,64)'*data2;
data_exp1 = 2*data_exp1(:) - 1;
data_exp2 = 2*data_exp2(:) - 1;

figure(1)
subplot(411)
plot(1:length(data_exp1),data_exp1,1:length(data_exp2),data_exp2)
axis([1,15*64,-2,2]);legend('数据1','数据2')

%--------------------------------------------------------------------------
%   然后我们可以将这些分散的信号加在一起，并通过AWGN信道进行传输编码
%   但是编码和噪声信号是不相关的
%--------------------------------------------------------------------------
%   生成用户编码
%--------------------------------------------------------------------------
codes = hadamard(64);
user_code1 = codes(:,35);
user_code2 = codes(:,44); 
%--------------------------------------------------------------------------
%   将用户编码加载到数据上
%--------------------------------------------------------------------------
spread1 = zeros(1,15*64);
spread2 = zeros(1,15*64);
count = 0; 
for idx = 1:15
    spread1((count+1):(count+64)) = data_exp1((count+1):(count+64)).*user_code1;
    spread2((count+1):(count+64)) = data_exp2((count+1):(count+64)).*user_code2;
    count = count + 64;
end

%--------------------------------------------------------------------------
%   将噪声加到传播信息中,两路用户数据直接合成一路发射
%--------------------------------------------------------------------------
noise = 1*randn(1,15*64);
noisy_cdma = spread1+ spread2 + noise;

subplot(412)
plot(noisy_cdma);axis([1,15*64,-12,12]);title('2路数据+对应用户编码+噪声');

%--------------------------------------------------------------------------
%   然后我们可以像以前一样恢复每个用户的CDMA信号。编码和噪声信号是不相关的。
%   另一个用户信号的效果没有影响，因为这两个代码被选择为正交的。
%   这里我们再次忽略了同步问题
%--------------------------------------------------------------------------
%   恢复CDMA信号
%--------------------------------------------------------------------------
%   按照用户1编码解码
%--------------------------------------------------------------------------
count = 0;
for idx = 1:15
    noisy_data_desp1((count+1):(count+64)) = ...
    noisy_cdma((count+1):(count+64)).*user_code1';

    noisy_data_desp2((count+1):(count+64)) = ...
    noisy_cdma((count+1):(count+64)).*user_code2';
    count = count + 64; 
end

%--------------------------------------------------------------------------
%   求和
%--------------------------------------------------------------------------
count = 0;
for idx = 1:15
    noisy_data_rec1(idx) = sum(noisy_data_desp1((count+1):(count+64)))/64;
    noisy_data_rec2(idx) = sum(noisy_data_desp2((count+1):(count+64)))/64;
    count = count+64;
end 

%--------------------------------------------------------------------------
%   二值化
%--------------------------------------------------------------------------
for idx = 1:15
    if (noisy_data_rec1(idx) > 0)
        noise_rec1(idx) = 1;
    else
        noise_rec1(idx) = -1;
    end
    if (noisy_data_rec2(idx) > 0)
        noise_rec2(idx) = 1;
    else
        noise_rec2(idx) = -1;
    end
end 

%--------------------------------------------------------------------------
%   扩展后还原长度
%--------------------------------------------------------------------------
noisy_data_rec_exp1 = ones(1,64)'*noise_rec1;
noisy_data_rec_exp1 = noisy_data_rec_exp1(:);

noisy_data_rec_exp2 = ones(1,64)'*noise_rec2;
noisy_data_rec_exp2 = noisy_data_rec_exp2(:);

subplot(413)
plot(noisy_data_rec_exp1);
title('还原用户1数据')
axis([1,15*64,-2,2]) 
subplot(414)
plot(noisy_data_rec_exp2,'r');
title('还原用户2数据')
axis([1,15*64,-2,2]) 
